package com.sugon.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.sugon.consts.ErmsCodePrefix;
import com.sugon.dao.FactoryTypeDao;
import com.sugon.dao.InspectionOptionDao;
import com.sugon.entity.DevTypeEntity;
import com.sugon.entity.FactoryTypeEntity;
import com.sugon.entity.InspectionOptionEntity;
import com.sugon.service.DevTypeService;
import com.sugon.service.InspectionOptionService;
import com.sugon.utils.RRException;
import com.sugon.utils.StringUtils;
import com.sugon.utils.excel.ExcelImport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 巡检选项表Service实现类
 *
 * @author sugon
 * @date 2020-01-03 09:34:22
 */
@Service("inspectionOptionService")
public class InspectionOptionServiceImpl implements InspectionOptionService {

    @Autowired
    private InspectionOptionDao inspectionOptionDao;
    @Autowired
    private FactoryTypeDao factoryTypeDao;
    @Autowired
    private DevTypeService devTypeService;

    @Override
    public InspectionOptionEntity queryObject(String id) {
        return inspectionOptionDao.queryObject(id);
    }

    @Override
    public List<InspectionOptionEntity> queryList(Map<String, Object> map) {
        return inspectionOptionDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return inspectionOptionDao.queryTotal(map);
    }

    @Override
    public int save(InspectionOptionEntity inspectionOption) {
        inspectionOption.setId(RandomUtil.randomString(32));
        inspectionOption.setOptionCode(generateOptionCode());
        inspectionOption.setDelFlag("1");
        return inspectionOptionDao.save(inspectionOption);
    }

    @Override
    public int update(InspectionOptionEntity inspectionOption) {
        return inspectionOptionDao.update(inspectionOption);
    }

    @Override
    public int delete(String id) {
        return inspectionOptionDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return inspectionOptionDao.deleteBatch(ids);
    }

    @Override
    public List<InspectionOptionEntity> queryListById(Map<String, Object> map) {

        return inspectionOptionDao.queryList(map);
    }

    /**
     * 生成设备巡检库选项编码
     * 生成规格：从DB中取出最新编码，然后加一
     *
     * @return 设备巡检库选项编码
     */
    private String generateOptionCode() {
        String lastedOptionCode = inspectionOptionDao.getLastestOptionCode();
        return com.sugon.modules.utils.StrUtil.increase(lastedOptionCode, ErmsCodePrefix.INSPECTION_OPTION_CODE_PREFIX, 4, 1);
    }

    @Override
    public int queryTotalById(Map<String, Object> map) {
        return inspectionOptionDao.queryTotal(map);
    }

    @Override
    public int imports(MultipartFile file) {

        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        List<String[]> list = ExcelImport.getExcelData(file);
        List<InspectionOptionEntity> inspectionOptionEntityList = new ArrayList<>();
        ;
        Map<String, Object> map = new HashMap<>(2);
        map.put("type", 0);
        for (int i = 0; i < list.size(); i++) {
            int size = i + 1;
            if (size < list.size()) {
                String[] inspection = list.get(size);
                if (!StringUtils.isNullOrEmpty(inspection)) {
                    //巡检编码判断
                    String code = inspection[0];
                    code = StringUtils.isNotEmpty(code) ? code : generateOptionCode();

                    InspectionOptionEntity inspectionOptionEntity = new InspectionOptionEntity();
                    inspectionOptionEntity.setId(RandomUtil.randomString(32));
                    inspectionOptionEntity.setDelFlag("1");
                    inspectionOptionEntity.setOptionCode(code);
                    //选项名称
                    inspectionOptionEntity.setOptionName(inspection[1]);
                    // 设置供应厂商编码
                    String supply = inspection[2];
                    map.put("nameCode", supply);
                    FactoryTypeEntity fte = factoryTypeDao.queryObjectByMap(map).get(0);
                    String supplyCode = fte.getFaCode();
                    inspectionOptionEntity.setDevSupply(supplyCode);
                    // 设置设备类型编码
                    inspectionOptionEntity.setDevTypeS(inspection[3]);
                    // 设置设备类型名称
                    DevTypeEntity devType = devTypeService.queryObjectByCode(inspection[3]);
                    if (devType != null) {
                        inspectionOptionEntity.setDevTypeNameS(devType.getTypeName());
                    }
                    inspectionOptionEntity.setOptionRemark(inspection[4]);  //备注
                    inspectionOptionEntity.setCheckNorm(inspection[5]);   //检查标准
                    inspectionOptionEntity.setJudNorm(inspection[6]);  //判断标准
                    inspectionOptionEntity.setMethodMeans(inspection[7]);  //方法手段

                    inspectionOptionEntityList.add(inspectionOptionEntity);
                }
            }
        }
        return inspectionOptionDao.batchInsert(inspectionOptionEntityList);
    }
}
